package cn.ticket.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import cn.ticket.service.HttpUtil;
import cn.ticket.service.Utility;

@RequestMapping("didi")
@Controller
public class DidiApiController {
	
	private static String token="AQfJHJOr7ImGp1XiBMd4KS-f6T1FGMeWzRScFPtsjjFMjTsOAjEMRK-CpnZhe-Pg9W2WP9WiRIgiyt2xqKj8PCO9GdgQAOGEUFdzUbF69FIL4YKQPFfEOKDv73ZOZMJr6_2zt1-9qtfFrEzC7f8n3FMs5q7Mqy6cG49MVCTpmZ75DQAA__8%3D&uuid=DDED40C5A6D13A03A4B7D0D29B13F444";
	@RequestMapping(value="getByRouteChengdu",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByRouteChengdu(){
		long date_id=(new Date()).getTime()+1000*24*60*60;
		String url="http://api.didialift.com/beatles/api/route/driver/info?at_mb_mcc=460&networkType=WIFI&"
				+ "model=ASUS_Z00ADB&date_id="
				+ date_id
				+ "&appversion=5.0.14&mac=08%3A00%3A27%3A19%3A03%3Ad9&lng=103.4924039561952&suuid=B5FDADEBA207DAFAE745E0040E610C56_218"
				+ "&city_id=283&android_id=1002B5A9695B0000&token="
				+ token
				+ "&at_mb_mnc=07&route_id=201098696&at_wf_ssid=AndroidAP&at_wf_bssid=10%3A02%3AB5%3AA9%3A69%3A5B&lat=29.77216769009204&vcode=202&os=4.4.2"
				+ "&dviceid=734f553d60d0adc961ddc253449b9e67&at_mb_lac=61804&imei=8643940101002141A35810277DF9652A5BE761313986A8B"
				+ "&cpu=Processor%09%3A%20ARMv7%20Processor%20rev%200%20(v7l)&at_net_st=1&maptype=soso&at_mb_cid=173151233"
				+ "&wsgsig=08131B475E8D1A4851883DA117F0C0DD3F5A0092BBCBEEBE2F6D60FDDF71&locatePerm=1&locateTime=1489195758&bts_udid=864394010100214&uuid=D1577BB33D8330C46BE9DD1379B96962&datatype=1"
				+ "&channel=218&filter=0";
		Map<String, Object> result=HttpUtil.get(url);
		Object data=result.get("body");
		Gson gson = new Gson();
		Map<String, Object> result0=gson.fromJson((String) data,  new TypeToken<Map<String, Object>>(){}.getType());
	//	Map<String, Object> result1=gson.fromJson((String) (result0.get("section_list")),  new TypeToken<Map<String, Object>>(){}.getType());
		return Utility.gson.toJson(result0.get("section_list"));
	}
	@RequestMapping(value="getByRouteIdAndDateId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByRouteIdAndDateId(@RequestBody Map<String, String> args){		
		String dateId=args.get("dateId");
		String routeId=args.get("routeId");
		if (args.get("token")!=null&&!args.get("token").equals("")) {
			token=args.get("token");
		}
		String url="http://api.didialift.com/beatles/api/route/driver/info?at_mb_mcc=460&networkType=WIFI&"
				+ "model=ASUS_Z00ADB&date_id="
				+ dateId
				+ "&appversion=5.0.14&mac=08%3A00%3A27%3A19%3A03%3Ad9&lng=103.4924039561952&suuid=B5FDADEBA207DAFAE745E0040E610C56_218"
				+ "&city_id=19&android_id=1002B5A9695B0000&token="
				+ token
				+ "&at_mb_mnc=07&route_id="
				+ routeId
				+ "&at_wf_ssid=AndroidAP&at_wf_bssid=10%3A02%3AB5%3AA9%3A69%3A5B&lat=29.77216769009204&vcode=205&os=4.4.2"
				+ "&dviceid=734f553d60d0adc961ddc253449b9e67&at_mb_lac=35002&imei=8643940101002141A35810277DF9652A5BE761313986A8B"
				+ "&cpu=Processor%09%3A%20ARMv7%20Processor%20rev%200%20(v7l)&at_net_st=1&maptype=soso&at_mb_cid=173151233"
				+ "&wsgsig=08131B475E8D1A4851883DA117F0C0DD3F5A0092BBCBEEBE2F6D60FDDF71&locatePerm=1&locateTime="
				+ (new Date()).getTime()
				+ "&bts_udid=864394010100214&datatype=1"
				+ "&channel=73399&filter=0";
		Map<String, Object> result=HttpUtil.get(url);
		Object data=result.get("body");
		 Gson gson = new Gson();
		Map<String, Object> result0=gson.fromJson((String) data,  new TypeToken<Map<String, Object>>(){}.getType());
	//	Map<String, Object> result1=gson.fromJson((String) (result0.get("section_list")),  new TypeToken<Map<String, Object>>(){}.getType());
		return Utility.gson.toJson(result0.get("section_list"));
	}
	@RequestMapping(value="getByRouteLeshan",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByRouteLeshan(){
		long date_id=(new Date()).getTime();
		String url="http://api.didialift.com/beatles/api/route/driver/info?at_mb_mcc=460&networkType=WIFI&"
				+ "model=ASUS_Z00ADB&date_id="
				+ date_id
				+ "&appversion=5.0.14&mac=08%3A00%3A27%3A19%3A03%3Ad9&lng=103.4924039561952&suuid=B5FDADEBA207DAFAE745E0040E610C56_218"
				+ "&city_id=283&android_id=1002B5A9695B0000&token="
				+ token
				+ "&at_mb_mnc=07&route_id=201759160&at_wf_ssid=AndroidAP&at_wf_bssid=10%3A02%3AB5%3AA9%3A69%3A5B&lat=29.77216769009204&vcode=202&os=4.4.2"
				+ "&dviceid=734f553d60d0adc961ddc253449b9e67&at_mb_lac=61804&imei=8643940101002141A35810277DF9652A5BE761313986A8B"
				+ "&cpu=Processor%09%3A%20ARMv7%20Processor%20rev%200%20(v7l)&at_net_st=1&maptype=soso&at_mb_cid=173151233"
				+ "&wsgsig=08131B475E8D1A4851883DA117F0C0DD3F5A0092BBCBEEBE2F6D60FDDF71&locatePerm=1&locateTime=1489195758&bts_udid=864394010100214&uuid=D1577BB33D8330C46BE9DD1379B96962&datatype=1"
				+ "&channel=218&filter=0";
		Map<String, Object> result=HttpUtil.get(url);
		Object data=result.get("body");
		 Gson gson = new Gson();
		Map<String, Object> result0=gson.fromJson((String) data,  new TypeToken<Map<String, Object>>(){}.getType());
	//	Map<String, Object> result1=gson.fromJson((String) (result0.get("section_list")),  new TypeToken<Map<String, Object>>(){}.getType());
		return Utility.gson.toJson(result0.get("section_list"));
	}
	
	@RequestMapping(value="ticketByRouteIdAndTime",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String ticketByRouteIdAndTime(@RequestBody Map<String, String> args) throws UnsupportedEncodingException{
		String setupTime=args.get("setupTime");
		setupTime=URLEncoder.encode(setupTime);
		String orderId=args.get("orderId");
		long date_id=(new Date()).getTime();
		if (args.get("token")!=null&&!args.get("token").equals("")) {
			token=args.get("token");
		}
		String url="http://api.didialift.com/beatles/api/driver/order/strive?at_mb_mcc=460&networkType=WIFI&model=ASUS_Z00ADB&car_id=22279786"
				+ "&date_id="
				+ date_id
				+ "&view_sort=1&appversion=5.0.14&mac=08%3A00%3A27%3A19%3A03%3Ad9"
				+ "&lng=102.56484986825174&setup_time="
				+ setupTime
				+ "&order_id="
				+ orderId
				+ "&suuid=9F61760A3E5598D3ED6F8A91A23F962C_211&city_id=19&android_id=1002B5A9695B0000"
				+ "&token="
				+ token;
	//	url=URLEncoder.encode(url);
		Map<String, Object> result=HttpUtil.get(url);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="setToken",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String setToken(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		if (args.get("token")!=null) {
			token=args.get("token");
			result.put("status", 1001);
		}		
		return  Utility.gson.toJson(result);
	}
	@RequestMapping(value="getRoutes",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getRoutes(){
		String url="";
		
		return null;
	}
}
